﻿//-----------------------------------------------------------------------
// <copyright file="EventLogMessage.cs" company="Craig Longford (deltawolf7@gmail.com)">
//     Copyright (c) Craig Longford. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Delta.Mercury.Business
{
    using System;
    using System.Data;
    using Delta.Data.DataExpress;
    using Delta.Mercury.Business.Exceptions;

    /// <summary>
    /// Event Log Message
    /// </summary>
    [Serializable]
    public class EventLogMessage : IdentifierBase, IDataServicable
    {
        #region Members
        /// <summary>
        /// Event source
        /// </summary>
        private string eventSource;

        /// <summary>
        /// Event date and time
        /// </summary>
        private DateTime dateTime;

        /// <summary>
        /// Event message
        /// </summary>
        private string message;
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the EventLogMessage class.
        /// </summary>
        public EventLogMessage()
            : this(string.Empty, string.Empty)
        {
        }

        /// <summary>
        /// Initializes a new instance of the EventLogMessage class.
        /// </summary>
        /// <param name="eventSource">Event source</param>
        /// <param name="message">Event message</param>
        public EventLogMessage(string eventSource, string message)
            : this(eventSource, message, EventLogType.Information)
        {
        }

        /// <summary>
        /// Initializes a new instance of the EventLogMessage class.
        /// </summary>
        /// <param name="eventSource">Event source</param>
        /// <param name="message">Event message</param>
        /// <param name="type">Event type</param>
        public EventLogMessage(string eventSource, string message, EventLogType type)
            : base()
        {
            this.eventSource = eventSource;
            this.dateTime = DateTime.Now;
            this.message = message;
            this.Type = type;
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the message source
        /// </summary>
        public string Source
        {
            get
            {
                return this.eventSource;
            }

            set
            {
                if (value.Length < 1)
                {
                    throw new InvalidLengthException("Message source must be a minimum of 1 characters.");
                }

                if (value.Length > 30)
                {
                    throw new InvalidLengthException("Message source must be a maximum of 30 characters.");
                }

                this.eventSource = value;
            }
        }

        /// <summary>
        /// Gets the datetime of the event
        /// </summary>
        public DateTime DateTime
        {
            get
            {
                return this.dateTime;
            }
        }

        /// <summary>
        /// Gets or sets the message text
        /// </summary>
        public string Message
        {
            get
            {
                return this.message;
            }

            set
            {
                if (value.Length < 1)
                {
                    throw new InvalidLengthException("Message text must be a minimum of 1 characters.");
                }

                this.message = value;
            }
        }

        /// <summary>
        /// Gets or sets the event log message type
        /// </summary>
        public EventLogType Type { get; set; }
        #endregion

        #region Create Event Log Message
        /// <summary>
        /// Create and save new event log message
        /// </summary>
        /// <param name="source">Source for event</param>
        /// <param name="message">Event message</param>
        /// <param name="type">Event message type</param>
        public static void CreateEventLog(string source, string message, EventLogType type)
        {
            EventLogMessage msg = new EventLogMessage(source, message, type);
            msg.Save();
        }
        #endregion

        #region Fill Methods
        /// <summary>
        /// Fill object from database
        /// </summary>
        /// <param name="reader">Filled object</param>
        public void Fill(IDataReader reader)
        {
            this.Guid = Helper.ToGuid(reader["Guid"]);
            this.message = Helper.ToString(reader["Message"]);
            this.eventSource = Helper.ToString(reader["Source"]);
            this.dateTime = Helper.ToDateTime(reader["Datetime"]);
            this.Type = Helper.ToEnum<EventLogType>(reader["Type"]);
        }
        #endregion

        #region Save Method
        /// <summary>
        /// Save the event message to database
        /// </summary>
        public void Save()
        {
            DataAccess.NonQuery("SaveEventLog", CommandType.StoredProcedure, GetSaveParameters());
        }
        #endregion

        #region Get Save Parameters
        /// <summary>
        /// Get save parameters
        /// </summary>
        /// <returns>Parameter collection</returns>
        public Parameters GetSaveParameters()
        {
            Parameters parmaters = new Parameters();
            parmaters.Add(new Parameter("Guid", this.Guid));
            parmaters.Add(new Parameter("Source", this.Source));
            parmaters.Add(new Parameter("Datetime", this.DateTime));
            parmaters.Add(new Parameter("Message", this.Message));
            parmaters.Add(new Parameter("Type", this.Type));
            return parmaters;
        }
        #endregion
    }
}
